<?php


namespace App\Http\Controllers\Api\V1\Front;


use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\User;
use App\Models\Merchant;
use App\Models\Casus;
use App\Models\Combo;
use App\Models\PostBanner;
use App\Models\PostBannerShop;
use App\Models\userLikeKeyValue;
use App\Models\UserFollow;
use App\Models\Comment;
use App\Models\CommentReply;
use App\Models\CommentLike;
use App\Models\UserLike;
use App\Models\UserCollect;
use App\Models\UserApply;
use App\Models\PrivateLetter;
use App\Models\PrivateContent;
use App\Models\KeyWord;
use App\Models\Paragraph;
use App\Models\Applet;
use App\Services\Front\IndexService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AppidController extends Controller
{
    /**
     * 获得商家id
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getMerchantId(Request $request)
    {
        $appId = $request->appId;
        if(!$appId){
            return $this->apiResponse('',config('errorLiu.NOTCANSHUID'));
        }
        if($appId == env("APPID")){
            return $this->apiResponse(null);
        }
        $merchant = User::select('t2.merchantId','t2.status as merchantStatus','t1.status as userStatus','t3.is_binding','t3.appId','t3.saleStatus')
                        ->from((new User())->getTable().' as t1')
                        ->join((new Merchant())->getTable().' as t2','t1.merchantId','=','t2.merchantId')
                        ->join((new Applet())->getTable().' as t3','t1.merchantId','=','t3.merchantId')
                        ->where('t1.is_index',0)
                        ->where('t3.appId',$appId)
                        ->where('t2.comboType',"ONONE")
                        ->first();
        if(!$merchant){
            return $this->apiResponse('',config('errorLiu.NOTAPPIDUSERMEACHER'));
        }
        if(!$merchant['userStatus']){
            return $this->apiResponse('',config('errorLiu.XIAJAISHANGJIA'));
        }
        if(!$merchant['merchantStatus']){
            return $this->apiResponse('',config('errorLiu.NOTAPPIDUSERMEACHER'));
        }
        if(!$merchant['saleStatus']){
            return $this->apiResponse('',config('errorLiu.XIAOCHENGXUWEIFABUSALE'));
        }
        return $this->apiResponse($merchant);
    }

    /**
     * 小程序绑定商家
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function setApplet(Request $request)
    {
        $appId = $request->appId;
        $userId = $request->USERID;
        $idCard = $request->idCard;
        if(!$appId || !$idCard){
            return $this->apiResponse('',config('errorLiu.NOTCANSHUID'));
        }
        $indexUser = User::where('idCard',$idCard)->where('is_index',1)->first();
        if(!$indexUser){
            return $this->apiResponse('',config('errorLiu.NOTIDCARDMEIYOUREN'));
        }
        $merchant = Merchant::where('merchantId',$indexUser['merchantId'])->where('idCard',$idCard)->first();
        if(!$merchant){
            return $this->apiResponse('',config('errorLiu.MERCHANXINXIWUFAXIA'));
        }
        if($merchant['comboType'] != "ONONE"){
            return $this->apiResponse('',config('errorLiu.BNOTDULISHANGJAI')); 
        }
        $applet = Applet::where('merchantId',$merchant['merchantId'])->first();
        if($applet){
            return $this->apiResponse('',config('errorLiu.CHONGFUBANGDINGBIERENDE')); 
        }
        $update = [];
        $update['updateTime'] = date("Y-m-d H:i:s");
        $update['merchantId'] = $merchant['merchantId'];
        $update['idCard'] = $idCard;
        User::where('userId',$userId)->update($update);
        $update = [];
        $update['merchantId'] = $merchant['merchantId'];
        $update['is_binding'] = 1;
        Applet::where('appId',$appId)->update($update);

        return $this->apiResponse(true);
    }
}